﻿namespace 燃油加注机指令翻译
{
    internal class Addition
    {
        [HexMapping(0, 6)]
        public string Header { get; set; } = "FC FC FC";

        [HexMapping(6, 2)]
        public string Control { get; set; } = "02";

        [HexMapping(8, 2)]
        public string FuelGun { get; set; } = "01";

        [HexMapping(10, 6)]
        public string? Litter { get; set; }

        [HexMapping(16, 6)]
        public string? Amount { get; set; }

        [HexMapping(22, 6)]
        public string? Kilogram { get; set; }

        [HexMapping(28, 6)]
        public string? Price { get; set; }

        [HexMapping(34, 8)]
        public string? Reciprocal { get; set; }

        [HexMapping(42, 2)]
        public string? Flag { get; set; }

        [HexMapping(44, 4)]
        public string? Chain { get; set; }

        [HexMapping(48, 2)]
        public string? CheckSum { get; set; }

        public string instructionsHexSimple()
        {
            return Header + " " + Control + " " + FuelGun + " " + Litter + " " + Amount + " " + Kilogram + " " + Price + " " + Reciprocal + " " + Flag + " " + Chain;
        }

        public string instructionsHex()
        {
            return instructionsHexSimple() + " " + CalculateChecksum(instructionsHexSimple());
        }


        string CalculateChecksum(string controlStr)
        {
            byte[] data = Tool.HexStringToByteArray(controlStr.Replace(" ", "").Substring(6));
            byte checksum = 0;
            foreach (byte b in data)
            {
                checksum ^= b;
            }
            return checksum.ToString("X2");
        }

        //string CalculateChecksumExists(string controlStr)
        //{
        //    byte[] data = HexStringToByteArray(controlStr.Substring(6, controlStr.Length - 8));
        //    byte checksum = 0;
        //    foreach (byte b in data)
        //    {
        //        checksum ^= b;
        //    }
        //    return checksum.ToString("X2");
        //}

    }
}
